#pragma once
//============================================================================
// Copyright (C) 2008-2013 Brett R. Jones 
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses 
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include <VxUtilLib/VxSktCode.h>
#include <VxUtilLib/CheezyCrypt.h>


class VxSktConnectSimple : public VxSktCode
{
public:
	//=== vars ===//
	//=== skt vars ===//
	SOCKET					m_oSocket;				// handle to socket

	VxInetAddrAndPort		m_oLclIp;				// local ip address
	std::string				m_strLclIp;				// local ip address in dotted form

	VxInetAddrAndPort		m_oRmtIp;				// remote (peer) ip address
	std::string				m_strRmtIp;				// remote (peer) ip address in dotted form

	//=== state vars ===//
	BOOL					m_bIsConnected;			// return true if is connected

	//=== encryption vars ===//
	VxKey					m_oTxKey;				// encryption key for transmit
	VxKey					m_oRxKey;				// encryption key for receive
	VxCrypto				m_oTxCrypto;			// encryption object for transmit
	VxCrypto				m_oRxCrypto;			// encryption object for receive

	//=== user vars ===//
	U64						m_u64UserData;			// user setable data

	//=== constructor ===//
	VxSktConnectSimple();
	//=== destructor ===//
	~VxSktConnectSimple();

	//! return true if is connected
	virtual BOOL			IsConnected( void );

	//! Connect to 
	virtual SOCKET			ConnectTo(	const char *	pIpOrUrl,						// remote ip or url
										U16				u16Port,						// port to connect to
										int				iTimeoutMilliSeconds = 8000 );	// timeout attempt to connect
	//! send data 
	virtual RCODE			SendData(	const char *	pData,							// data to send
										int				iDataLen,						// length of data	
										int				iTimeoutMilliSeconds = 3000 );	// timeout attempt to send ( 0 = don't timeout )

	//! receive data.. if timeout is set then will keep trying till buffer is full or error or timeout expires
	virtual RCODE			RecieveData(	char *		pRetDataBuf,					// data buffer to read into
											int			iBufLen,						// length of data	
											int *		iRetBytesRecieved,				// number of bytes actually received
											int			iTimeoutMilliSeconds = 3000,	// timeout attempt to received
											BOOL		bAbortIfCrLfCrLf = false,		// if true then abort receive when \r\n\r\n is received
											BOOL *		pbRetGotCrLfCrLf = NULL );		// if received \r\n\r\n set to true
	//! Close the socket
	virtual void			CloseSkt( int iInstance = 0 );

	//! get the sockets peer connection ip address as host order U32
	virtual RCODE			GetRemoteIp(	VxInetAddress& u32RetIp,		// return ip
											U16 &u16RetPort );	// return port
	//! get remote ip as string
	virtual const char *	GetRemoteIp( void );
	//! simpler version of GetRemoteIp returns ip as host order U32
	virtual RCODE			GetRemoteIp( VxInetAddress &u32RetIp );			// return ip
	//! get remote port connection is on
	virtual VxInetAddress	GetRemoteIpBinary( void ){ return m_oRmtIp;}			// return ip in host ordered binary u32
	//! get local ip as string
	virtual const char *	GetLocalIp( void )		{ return m_strLclIp.c_str(); }
	//! get local ip as string
	virtual VxInetAddress	GetLocalIpBinary( void ){ return m_oRmtIp;};

	//! used internally to get the remote ip address and set it in the vars
	RCODE					DoGetRmtAddress( void );

	//! encrypt then send data using session crypto
	RCODE					TxEncrypted(	const char *	pDataIn, 		// data to send
											int				iDataLen,		// length of data
											BOOL			bDisconnect );	// if true disconnect after send
};

